<template>
	<view class="content">
		<tabsTop tabName="我的券码"></tabsTop>
		<view v-if="couponArr.length > 0" class="coupon_content">
			<scroll-view scroll-y="true">
				<view v-for="(item,index) in couponArr" :index="index" :key="index">
					<view class="row flex justify-center">
						<coupon :couponName="item.Coupon_Scheme_Name"
							:couponRule="item.Coupon_Scheme_Rule" :Min_Amount="item.Min_Amount"
							:Preferential="item.Preferential"></coupon>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 无优惠券 -->
		<view v-else class="noOffers">
			<view style="font-size: 13px;">暂无优惠券码</view>
			<button>进店逛逛</button>
		</view>
		<!-- 底部失效券码和兑换 -->
		<view class="lapse flex" @click="goExpired">查看失效的券码<uni-icons type="right" size="10" class="flex align-center"></uni-icons></view>
		<view class="fixed-button shadow" @click="openPopup">兑换优惠码</view>
		<view v-if="popupVisible" class="popup-mask">
			<view class="popup">
				<view class="popup-header">
					<view></view>
					<span>兑换优惠码</span>
					<uni-icons @click="closePopup" type="closeempty" size="20"></uni-icons>
				</view>
				<view class="popup-content">
					<input v-model="exchangeCode" type="text" placeholder="请输入优惠码" />
				</view>
				<view class="popup-footer">
					<view class="popup_but" :disabled="exchangeCode === ''" @click="exchangeCoupon">兑换</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import {getmemberCouponAction} from "@/api/coupon.js"
	import tabsTop from "@/components/tabs/tabs-top.vue";
	import tabsBottom from "@/components/tabs/tabs-bottom.vue";
	import coupons from "@/components/coupons/coupons.vue";
	import coupon from "@/components/tool/coupon.vue"
	export default {
		components: { //轮播图组件
			tabsTop,
			tabsBottom,
			coupons,
			coupon
		},
		data() {
			return {
				popupVisible: false,
				exchangeCode: '',
				couponArr:[],
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			openPopup() {
				this.popupVisible = true;
			},
			closePopup() {
				this.popupVisible = false;
				this.exchangeCode = '';
			},
			exchangeCoupon() {
				// 实现兑换逻辑的代码
				console.log('兑换码：', this.exchangeCode);
				// 这里可以编写处理兑换逻辑的代码
				// 成功后关闭弹窗等操作
				this.closePopup();
			},
			goExpired(){
				uni.navigateTo({
					url: '/pages/my/expiredCoupons'
				})
			},
			async getData(){
				const res = await getmemberCouponAction({
					page: 1,
					limit: 10,
					Open_ID: app.globalData.Open_ID,
					App_ID: app.globalData.App_ID,
					Enterprise_Info_SerialNumber: app.globalData.Enterprise_Info_SerialNumber,
					Type: 0,
				})
				if (res.Status == "success") {
					this.couponArr = res.tableData;
				}
			}
		}
	}
</script>

<style>
	.fixed-button {
		position: fixed;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		padding: 10px 20px;
		cursor: pointer;
		width: 100%;
		text-align: center;
	}

	.popup-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 9999;
	}

	.popup {
		width: 80%;
		max-width: 400px;
		background-color: #fff;
		border-radius: 10px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.popup-header {
		padding: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.popup-content {
		padding: 10px;
	}

	.popup-footer {
		padding: 10px;
		text-align: center;
	}

	input {
		width: 85%;
		padding: 7px;
		border: 1px solid #ccc;
		border-radius: 6px;
		background-color: #f8f9fa;
		margin: auto;
	}

	.popup_but {
		padding: 6px 12px;
		background-color: #f8f9fa;
		border-radius: 20px;
		cursor: pointer;
		border: 1px solid #f8f9fa;
		width: 40%;
		margin: auto;
	}

	.popup_but:disabled {
		background-color: #ccc;
		cursor: not-allowed;
	}

	.lapse {
		position: absolute;
		bottom: 60px;
		left: 153px;
		font-size: 10px;
	}

	button {
		font-size: 12px;
		padding: 0;
		color: red;
		border-radius: 18px;
		border: 1px solid;
		margin-top: 18px;
	}
	
	.coupon_content scroll-view {
		min-height: 580rpx;
	}
	.row {
		padding: 20rpx 20rpx 0 20rpx;
		box-sizing: border-box;
		display: flex;
	}
	
	.noOffers{
		position: absolute;
		top: 46%;
		left: 50%;
		transform: translate(-50%,-50%);
	}
</style>